"""
管理类
"""
from db import db_handler


class Base:
    # 注册，保存，更新
    def save(self):
        db_handler.save_data(self)

    # 查询
    @classmethod
    def select(cls, username):
        return db_handler.select_data(cls, username)


class Admin(Base):
    def __init__(self, username, password):
        self.username = username
        self.password = password

    # 创建学校
    def create_school(self, school_name, school_address):
        School(school_name, school_address).save()

    # 创建课程
    def create_course(self, school_obj, course_name):
        Course(course_name).save()
        school_obj.course_list.append(course_name)
        school_obj.save()

    # 创建老师
    def create_teacher(self, teacher_name, teacher_pwd):
        Teacher(teacher_name, teacher_pwd).save()


class School(Base):
    def __init__(self, school_name, school_address):
        # 必须写username 因为 db_handler.select_data 查询规范
        self.username = school_name
        self.address = school_address
        self.course_list = []


class Student(Base):
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.school = None
        self.course_list = []
        self.score = {}

    def select_school(self, school_name):
        self.school = school_name
        self.save()

    def select_course(self, course_name):
        # 学生绑定课程
        self.course_list.append(course_name)
        # 课程添加分数
        self.score[course_name] = 0
        self.save()
        # 课程添加学生
        course_obj = Course.select(course_name)
        course_obj.student_list.append(self.username)
        course_obj.save()


class Course(Base):
    def __init__(self, course_name):
        # 必须写username 因为 db_handler.select_data 查询规范
        self.username = course_name
        self.student_list = []


class Teacher(Base):
    def __init__(self, teacher_name, teacher_pwd):
        # 必须写username 因为 db_handler.select_data 查询规范
        self.username = teacher_name
        self.password = teacher_pwd
        self.course_list=[]

    def check_course(self):
        return self.course_list

    def select_course(self, course_name):
        # 绑定课程
        self.course_list.append(course_name)
        self.save()

    def get_student(self,course_name):
        return Course.select(course_name).student_list

    def manage_student(self,student_obj,course_name,score):
        student_obj.score[course_name]=score
        student_obj.save()
